package com.rk.kbk.controller;

import com.rk.kbk.common.JsonResp;
import com.rk.kbk.dao.WorkLogDao;
import com.rk.kbk.dto.PageDto;
import com.rk.kbk.model.WorkLog;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.math.BigInteger;
import java.util.List;

/**
 * @WorkLogController
 * @骑手请假Controller
 * @version : Ver 1.0
 */
@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/workLog")
public class WorkLogController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private WorkLogDao workLogDao;
    @PersistenceContext
    private EntityManager entityManager;

    /**
     * @添加/修改骑手请假
     * @param workLog
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdateWorkLog",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdateWorkLog(@RequestBody WorkLog workLog){
        log.debug("添加/修改骑手请假");
        workLogDao.save(workLog);
        return JsonResp.ok();
    }

    /**
     * @批量添加/修改骑手请假
     * @param workLogs
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addOrUpdateSomeWorkLog",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addOrUpdateSomeWorkLog(@RequestBody List<WorkLog> workLogs){
        log.debug("批量添加/修改骑手请假");
        workLogDao.save(workLogs);
        return JsonResp.ok();
    }

    /**
     * @获取骑手请假详情-ByWorkLogId
     * @param workLogId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/workLogDetails",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findWorkLogDetailsByWorkLogId(Long workLogId) {
        log.debug("获取骑手请假详情-ByWorkLogId");
        return JsonResp.ok(workLogDao.findOne(workLogId));
    }

    /**
     * @分页查询骑手请假(jpa方式)
     * @param workLog 分页条件
     * @param pageNum 第几页
     * @param pageSize 每页的条数
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/jpa/page",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findPageWorkLogJpa(@RequestBody(required = false) WorkLog workLog, Integer pageNum, Integer pageSize) {
        log.debug("分页查询骑手请假(jpa方式)");
        //多条件排序Sort
        //Sort sort = new Sort(Sort.Direction.DESC, "createdate").and(new Sort(Sort.Direction.AES, "id"));
        //排序Sort
        Sort sort = new Sort(Sort.Direction.DESC, "gmtDatetime");
        Pageable pageable = new PageRequest(pageNum-1, pageSize, sort);
        Page page = null;
        if (null == workLog) {
            page = workLogDao.findAll(pageable); //无条件分页
        }/*else{
            final String condition =  workLog.getUserName();
            page = workLogDao.findAll( 		  //条件分页
                    (root, criteriaQuery, cb) -> {
                        Predicate predicate = cb.conjunction();
                        List<Expression<Boolean>> expressions = predicate.getExpressions();
                        if (StringUtils.isNotBlank(condition)) {
                            expressions.add(cb.like(root.<String>get("condition"), "%"+ condition +"%"));
                        }
                        return predicate;
                    }, pageable);
        }*/
//        List<WorkLog> content = page.getContent();
//        for (WorkLog workLog1 : content) {
//        }
        return JsonResp.ok(new PageDto(page));
    }

    /**
     * @删除骑手请假-one(永久)
     * @param workLogId
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteWorkLogOne",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteWorkLogOne(Long workLogId) {
        log.debug("删除骑手请假-one(永久)");
        workLogDao.delete(workLogId);
        return JsonResp.ok();
    }

    /**
     * @批量删除骑手请假(永久)
     * @param workLogs
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteSomeWorkLog",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteSomeWorkLog(@RequestBody List<WorkLog> workLogs) {
        log.debug("批量删除骑手请假(永久)");
        workLogDao.delete(workLogs);
        return JsonResp.ok();
    }


}
